package Dao.Process.Imp;

import Dao.Process.TaskDetailDao;

import Entity.Process.TaskDetail;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TaskDetailDaoImp implements TaskDetailDao {
    private PreparedStatement ps;
    private ResultSet rs;

    @Override
    public Integer insetTaskDetail(TaskDetail taskDetail, Connection conn) {
        int result = 0;
        try {
            String sql = "insert into task_detail " +
                    "(task_id,starttime, `require`, paper_number, matter, datum, execute_time, finish_time, " +
                    "comtent1, comtent1_time, comtent2, comtent2_time, comtent3, comtent3_time, comtent4, comtent4_time, comtent5, comtent5_time ) " +
                    "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, taskDetail.getTaskId());
            ps.setString(2, taskDetail.getStarttime());
            ps.setString(3, taskDetail.getRequire());
            ps.setString(4, taskDetail.getPaperNumber());
            ps.setString(5, taskDetail.getMatter());
            ps.setString(6, taskDetail.getDatum());
            ps.setString(7, taskDetail.getExecuteTime());
            ps.setString(8, taskDetail.getFinishTime());
            ps.setString(9, taskDetail.getComtent1());
            ps.setString(10, taskDetail.getComtent1Time());
            ps.setString(11, taskDetail.getComtent2());
            ps.setString(12, taskDetail.getComtent2Time());
            ps.setString(13, taskDetail.getComtent3());
            ps.setString(14, taskDetail.getComtent3Time());
            ps.setString(15, taskDetail.getComtent4());
            ps.setString(16, taskDetail.getComtent4Time());
            ps.setString(17, taskDetail.getComtent5());
            ps.setString(18, taskDetail.getComtent5Time());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;

    }

    @Override
    public TaskDetail getTaskDetailById(Integer id, Connection conn) {
        TaskDetail taskDetail = null;
        try {
            String sql = "select  " +
                    "task_id,starttime, `require`, paper_number, matter, datum, execute_time, finish_time, " +
                    "comtent1, comtent1_time, comtent2, comtent2_time, comtent3, comtent3_time, comtent4, comtent4_time, comtent5, comtent5_time " +
                    " from task_detail where task_id = ? ";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Integer taskId = rs.getInt(1);
                String starttime = rs.getString(2);
                String require = rs.getString(3);
                String paperNumber = rs.getString(4);
                String matter = rs.getString(5);
                String datum = rs.getString(6);
                String executeTime = rs.getString(7);
                String finishTime = rs.getString(8);
                String comtent1 = rs.getString(9);
                String comtent1Time = rs.getString(10);
                String comtent2 = rs.getString(11);
                String comtent2Time = rs.getString(12);
                String comtent3 = rs.getString(13);
                String comtent3Time = rs.getString(14);
                String comtent4 = rs.getString(15);
                String comtent4Time = rs.getString(16);
                String comtent5 = rs.getString(17);
                String comtent5Time = rs.getString(18);
                taskDetail =
                        new TaskDetail(taskId, starttime, require, paperNumber, matter, datum, executeTime, finishTime,
                                comtent1, comtent1Time, comtent2, comtent2Time, comtent3, comtent3Time, comtent4, comtent4Time, comtent5, comtent5Time);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return taskDetail;
    }

    @Override
    public Integer updateTaskDetailById(TaskDetail taskDetail, Connection conn) {
        int res = 0;
        try {
            String sql = "update task_detail set " +
                    "starttime = ?, `require` = ?, paper_number = ?, matter = ?, datum = ?, execute_time = ?, finish_time = ?, " +
                    "comtent1 = ?, comtent1_time = ?, comtent2 = ?, comtent2_time = ?, comtent3 = ?, comtent3_time =?, " +
                    "comtent4 = ?, comtent4_time = ?, comtent5 = ?, comtent5_time = ? " +
                    " where task_id = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, taskDetail.getStarttime());
            ps.setString(2, taskDetail.getRequire());
            ps.setString(3, taskDetail.getPaperNumber());
            ps.setString(4, taskDetail.getMatter());
            ps.setString(5, taskDetail.getDatum());
            ps.setString(6, taskDetail.getExecuteTime());
            ps.setString(7, taskDetail.getFinishTime());
            ps.setString(8, taskDetail.getComtent1());
            ps.setString(9, taskDetail.getComtent1Time());
            ps.setString(10, taskDetail.getComtent2());
            ps.setString(11, taskDetail.getComtent2Time());
            ps.setString(12, taskDetail.getComtent3());
            ps.setString(13, taskDetail.getComtent3Time());
            ps.setString(14, taskDetail.getComtent4());
            ps.setString(15, taskDetail.getComtent4Time());
            ps.setString(16, taskDetail.getComtent5());
            ps.setString(17, taskDetail.getComtent5Time());
            ps.setInt(18, taskDetail.getTaskId());
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }
}
